package com.enjoy.book.action;
import com.alibaba.fastjson.JSON;
import com.enjoy.book.bean.Book;
import com.enjoy.book.biz.BookBiz;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;
@WebServlet("/book.let")
/**
 * @author 陈俊威
 */
public class BookServlet extends HttpServlet {
    BookBiz  bookBiz = new BookBiz();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * 地址
     * /book.let?type=add 添加图书
     * /book.let?type=modifypre&id=xx 修改前准备
     * /book.let?type=modify        修改
     * /book.let?type=remove&id=xx    删除
     * /book.let?type=query&pageIndex=1 :分页查询(request:转发)
     * /book.let?type=details&id=xx   展示书籍详细信息
     * /book.let?type=doajax&name=xx  :使用ajax查询图书名对应的图书信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter  out  = resp.getWriter();
        //验证用户是否登录

        String state= req.getSession().toString();
        if(state==null){
           out.println("<script>alert('请登录');parent.window.location.href='login.html';</script>");
           return;
        }
        String type = req.getParameter("type");
        switch (type){
            case "add":
                try {
                    add1(req,resp,out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "modifypre":
                //book.let?type=modifypre&id=xx
                long bookId = Long.parseLong(req.getParameter("id"));
                Book book = bookBiz.getById(bookId);
                req.setAttribute("book",book);
                req.getRequestDispatcher("book_modify1.jsp").forward(req,resp);
                break;
            case "modify":
                try {
                    modify1(req,resp,out);
                  //  modify(req,resp,out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "remove":
                //1.获取删除的bookid
                long removeId = Long.parseLong(req.getParameter("id"));

                //2. 调用biz删除方法
                try {
                    int count =  bookBiz.remove(removeId);
                    if(count>0){
                        out.println("<script>alert('图书删除成功'); location.href='book.let?type=query&pageIndex=1';</script>");
                    }else{
                        out.println("<script>alert('图书删除失败'); location.href='book.let?type=query&pageIndex=1';</script>");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    out.println("<script>alert('"+e.getMessage()+"'); location.href='book.let?type=query&pageIndex=1';</script>");
                }
                //3. 提示+跳转=>out (查询的servlet)
                break;
            case "query":
                query(req,resp,out);
                break;
            case "details":
                details(req,resp,out);
                break;
            case "doajax":
                String name = req.getParameter("name");
                Book book2 = bookBiz.getByName(name);
                if(book2==null){
                    out.print("{}");
                }else{
                    out.print(JSON.toJSONString(book2));
                }
                break;


            default:
                 resp.sendError(404);
        }
    }

    /**
     * 修改图书信息的方法
     * @param req
     * @param resp
     * @param out
     */
    private void add1(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws Exception {
        Book book =new Book();
        book.setName(req.getParameter("name"));
        book.setAddress(req.getParameter("address"));
        book.setDesc(req.getParameter("desc"));
        double price = Double.parseDouble(req.getParameter("price"));
        book.setPrice(price);
        long  stock=Long.parseLong(req.getParameter("stock"));
        book.setStock(stock);
        book.setPublish(req.getParameter("publish"));
        book.setAuthor(req.getParameter("author"));
        int count = bookBiz.add(book);
        if(count>0){
            out.println("<script>alert('添加书籍成功');location.href='book.let?type=query&pageIndex=1';</script>");
        }else{
            out.println("<script>alert('添加书籍失败');location.href='book_add.jsp';</script>");
        }

    }
    private void modify1(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws Exception ,ClassCastException{

        Book book=new Book();
        book.setName(req.getParameter("name"));
        book.setAddress(req.getParameter("address"));
        long id =Long.parseLong(req.getParameter("id"));
        book.setId(id);
        book.setDesc(req.getParameter("desc"));
        double price = Double.parseDouble(req.getParameter("price"));
        book.setPrice(price);
        long  stock=Long.parseLong(req.getParameter("stock"));
        book.setStock(stock);
        book.setPublish(req.getParameter("publish"));
        book.setAuthor(req.getParameter("author"));
        System.out.println(book.toString());
        int count = bookBiz.modify(book);
        if(count>0){
            out.println("<script>alert('修改书籍成功');location.href='book.let?type=query&pageIndex=1';</script>");
        }else{
            out.println("<script>alert('该书不存在,修改书籍失败');location.href='book.let?type=query&pageIndex=1';</script>");
        }
    }


    /**
     * 查看图书详情
     * @param req
     * @param resp
     * @param out
     */

    private void details(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws ServletException, IOException {
        //1.获取图书的编号
         long bookId =  Long.parseLong(req.getParameter("id"));
        //2.根据编号获取图书对象
         Book book = bookBiz.getById(bookId);
        //3.将对象保存到req
        req.setAttribute("book",book);
        //4.转发到 jsp页面
        req.getRequestDispatcher("book_details.jsp").forward(req,resp);
    }

    /**
     * 查询
     * book.let?type=query&pageIndex=1
     * 页数: biz
     * 当前页码:pageIndex = 1
     * 存:request,转发
     * @param req
     * @param resp
     * @param out
     */

    private void query(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws ServletException, IOException {
        //1.获取信息(页数，页码,信息)
        int pageSize = 10;
        int pageCount = bookBiz.getPageCount( pageSize);
        int pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
        if(pageIndex<1){
            pageIndex = 1;
        }
        if(pageIndex>pageCount){
            pageIndex = pageCount;
        }
        List<Book> books = bookBiz.getByPage(pageIndex,pageSize);
        //2.存
        req.setAttribute("pageCount",pageCount);
        req.setAttribute("books",books);
        //3. 转发到jsp页面，通过地址栏传页数
        req.getRequestDispatcher("book_list.jsp?pageIndex="+pageIndex).forward(req,resp);
    }
}
